package cn.aloxc.stock
import cn.aloxc.stock.context.SysContext;
import cn.aloxc.stock.tools.XlsExportService;
import java.text.SimpleDateFormat;
class BoyistatController {

    def index = {
        redirect action:"list", params:params 
    }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
  def ed = {
    //导出数据为execl文件
         XlsExportService xlsExportService = new XlsExportService();  
         response.setHeader("Content-disposition", "attachment; filename=boyistat.xls")
         response.setContentType("application/vnd.ms-excel")
         xlsExportService.xlsExport(response.outputStream, request,"Zcggstat", Zcggstat.findAllByAtype("lianxijigoujiancanggegu",[max:100]));
          //xlsExportService.xlsExport(response.outputStream, request,"Boyistat", Boyistat.findAllByAtypeAndPeriod("jigoujingliuru","day",[max:30]));
         //redirect(action:"sdd",params:params)
          //render Boyistat.findAllByAtypeAndPeriod("jigoujingliuru","day",[max:30])
         //render request.toString()+"--------"+(new Date()).toString()+"${xlsExportService.getClass().getName()}";
     }

    def list = {
      redirect(action:"sdd",params:params)
      /*
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ boyistatInstanceList: Boyistat.list( params ), boyistatInstanceTotal: Boyistat.count() ]
        */
    }
    def sdd = {      
      params.max = params.max ? (params.max.toInteger() > SysContext.BOYI_SDD_MAX ? SysContext.BOYI_SDD_MAX : params.max.toInteger()) : SysContext.BOYI_SDD_MAX;
      params.a = params.a ? SysContext.BOYI_ATYPE[(params.a.toInteger() > SysContext.BOYI_ATYPE.size() ? 0 : params.a.toInteger())] : SysContext.BOYI_ATYPE[0];
      params.p = params.p ? SysContext.BOYI_PERIOD[(params.p.toInteger() > SysContext.BOYI_PERIOD.size() ? 0 : params.p.toInteger())] : SysContext.BOYI_PERIOD[0];
      params.s = params.s ? params.s.toInteger() : SysContext.BOYI_SDD_DAYS;
      def curDate = new Date() - params.s;
      def boyistatdataMap = new TreeMap();
      def sdf = new SimpleDateFormat("yyMMdd");
      0.step(params.s,1){
        curDate++;
        curDate.setHours(0);
        curDate.setMinutes(0);
        curDate.setSeconds(0);
        def criteria = Boyistat.createCriteria();
        def cList,key;
        cList = criteria.list{
          eq("atype",params.a);
          eq("period",params.p);          
          eq("statdate",curDate);
          maxResults(params.max);
          order("id","asc")
        };
        def fList = [];
        cList.each{
           fList.add(it);
        };
        key = sdf.format(curDate);
        if(fList.size()>0)
        {
          boyistatdataMap.put(key,fList)
        }
      };
      render view:'sdd', model:[boyistatInstanceMapList:boyistatdataMap]
    }

    def show = {
      redirect(action:"sdd",params:params)
      /*
        def boyistatInstance = Boyistat.get( params.id )

        if(!boyistatInstance) {
            flash.message = "Boyistat not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            [ boyistatInstance : boyistatInstance ]
        }
    */        
    }

    def delete = {
      redirect(action:"sdd",params:params)
      /*
        def boyistatInstance = Boyistat.get( params.id )
        if(boyistatInstance) {
            try {
                boyistatInstance.delete(flush:true)
                flash.message = "Boyistat ${params.id} deleted"
                redirect(action:"list")
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Boyistat ${params.id} could not be deleted"
                redirect(action:"show",id:params.id)
            }
        }
        else {
            flash.message = "Boyistat not found with id ${params.id}"
            redirect(action:"list")
        }
    */        
    }

    def edit = {
      redirect(action:"sdd",params:params)
      /*
        def boyistatInstance = Boyistat.get( params.id )

        if(!boyistatInstance) {
            flash.message = "Boyistat not found with id ${params.id}"
            redirect action:'list'
        }
        else {
            return [ boyistatInstance : boyistatInstance ]
        }
    */
    }

    def update = {
      redirect(action:"sdd",params:params)
      /*
        def boyistatInstance = Boyistat.get( params.id )
        if(boyistatInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(boyistatInstance.version > version) {
                    
                    boyistatInstance.errors.rejectValue("version", "boyistat.optimistic.locking.failure", "Another user has updated this Boyistat while you were editing.")

                    render view:'edit', model:[boyistatInstance:boyistatInstance]
                    return
                }
            }
            boyistatInstance.properties = params
            if(!boyistatInstance.hasErrors() && boyistatInstance.save()) {
                flash.message = "Boyistat ${params.id} updated"

                redirect action:'show', id:boyistatInstance.id
            }
            else {
                render view:'edit', model:[boyistatInstance:boyistatInstance]
            }
        }
        else {
            flash.message = "Boyistat not found with id ${params.id}"
            redirect action:'list'
        }
    */
    }

    def create = {
      redirect(action:"sdd",params:params)
      /*
        def boyistatInstance = new Boyistat()
        boyistatInstance.properties = params
        return ['boyistatInstance':boyistatInstance]
    */
    }

    def save = {
      redirect(action:"sdd",params:params)
      /*
        def boyistatInstance = new Boyistat(params)
        if(boyistatInstance.save(flush:true)) {
            flash.message = "Boyistat ${boyistatInstance.id} created"

            redirect action:"show", id:boyistatInstance.id
        }
        else {
            render view:'create', model:[boyistatInstance:boyistatInstance]
        } */
    }

}
